package com.link.base.base.autoconfig.autoconfig.controller;

import com.link.base.base.autoconfig.autoconfig.model.LnkInnerLink;
import com.link.base.basic.model.ListOfValue;
import com.link.base.base.autoconfig.autoconfig.service.LnkInnerLinkService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.query.Filter;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller("LnkInnerLinkController")
@RequestMapping("/link/LnkInnerLink")
public class LnkInnerLinkController extends BasicController<LnkInnerLink> {

    @Resource
    private LnkInnerLinkService service;

    @Override
    public BasicService<LnkInnerLink> getBasicService() throws Exception {
        return service;
    }

    /**
     * 重写父类beforeUpsert方法校验表字段
     *
     * @param entity
     * @param request
     * @throws Exception
     * @author 姚彭健
     * @date 2019-07-17
     */
    @Override
    public void beforUpsert(LnkInnerLink entity, HttpServletRequest request) throws Exception {
        // 校验表字段长度是否超过20
        boolean checkPassed = StringUtils.checkTable(entity.getTablename(), 20);
        if (!checkPassed) {
            throw new ServiceException("AUTOCONFIG-001");
        }
    }

    @Override
    public void beforQueryAllOrExample(LnkInnerLink entity, HttpServletRequest request) throws Exception {
        super.beforQueryAllOrExample(entity, request);
        if (null == entity.getParentid()) {
            entity.setParentid(0L);
        }
    }

    @RequestMapping(value = "/getobjlink/get")
    @ResponseBody
    public Object getobjlink(@RequestParam("parentid") String parentid, HttpSession session, HttpServletRequest request,
                             HttpServletResponse response) {
        QueryParams qps = new QueryParams();
        qps.addFilter(new Filter("parentid", parentid));
        List<ListOfValue> list = null;
        try {
            list = service.queryObjectLink(qps);
        } catch (Exception e) {
            LCLogger.withException(e);
        }
        return list;
    }
}
